package com.asion.qkan.adapters;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.asion.qkan.R;
import com.asion.qkan.entry.MusicBean;
import com.asion.qkan.ui.contract.MusicContract;
import com.facebook.drawee.view.SimpleDraweeView;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.List;

/**
 * Created by asion on 16-10-12.
 */

public class MusicAdapter extends RecyclerView.Adapter <MusicAdapter.MyHolder>{
    private Context context;


    private List<MusicBean.ResultBean.TracksBean> list;

    public void setOnMusicItemClickListener(MusicContract.OnMusicItemClickListener onMusicItemClickListener) {
        this.onMusicItemClickListener = onMusicItemClickListener;
    }

    private MusicContract.OnMusicItemClickListener onMusicItemClickListener;

    public MusicAdapter(Context context) {
        this.context = context;
        list = new ArrayList<>();
    }

    @Override
    public MyHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.music_item, parent, false);
        return new MyHolder(view);
    }

    @Override
    public void onBindViewHolder(MyHolder holder, int position) {
        MusicBean.ResultBean.TracksBean tracksBean = list.get(position);
        if(tracksBean!=null){
            holder.musicTitle.setText(tracksBean.getName());
            for (MusicBean.ResultBean.TracksBean.AlbumBean.ArtistsBean artistsBean : tracksBean.getAlbum().getArtists()) {
                holder.musicArtist.setText(artistsBean.getName());
            }
            holder.musicTime.setText(progresstime(tracksBean.getDuration()));
            holder.musicImage.setImageURI(tracksBean.getAlbum().getPicUrl());
            holder.itemView.setOnClickListener(v->onMusicItemClickListener.onMusicItemClick(position,tracksBean));
        }
    }

    @Override
    public int getItemCount() {
        return list==null?0:list.size();
    }

    public static class MyHolder extends RecyclerView.ViewHolder{

        private SimpleDraweeView musicImage;
        private TextView musicTitle;
        private TextView musicArtist;
        private TextView musicTime;

        public MyHolder(View itemView) {
            super(itemView);
            musicImage = ((SimpleDraweeView) itemView.findViewById(R.id.music_image));
            musicTitle = ((TextView) itemView.findViewById(R.id.music_title));
            musicArtist = ((TextView) itemView.findViewById(R.id.music_artist));
            musicTime = ((TextView) itemView.findViewById(R.id.music_time));
        }
    }

    public void addAll(Collection collection){
        list.addAll(collection);
       notifyDataSetChanged();
    }

    public List adapterList(){
        return this.list;
    }

    private String progresstime(int progress)
    {
        Date date = new Date(progress);
        SimpleDateFormat format = new SimpleDateFormat("mm:ss");
        return format.format(date);
    }
}
